package com.mjh.controller;

import com.github.pagehelper.PageInfo;
import com.mjh.bean.User;
import com.mjh.model.R;
import com.mjh.model.SecurityUser;
import com.mjh.service.UserService;
import com.mjh.utils.AuthUtil;
import com.mjh.vo.PieData;
import com.mjh.vo.UserDetailInfoVo;
import com.mjh.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

/**
 * 用户接口管理
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/info")
    @PreAuthorize("hasAnyAuthority('user:view')")
    public R<SecurityUser> info(){
        //获取当前登录用户id
        Integer userId = AuthUtil.getLoginUserId();
        SecurityUser user = AuthUtil.getSecurityUser();
        userService.modifyTime(user);
        return R.OK(user);
    }
    /**
     * 查询用户全部信息
     * @return
     */
    @GetMapping("/infoUser")
    public R<UserDetailInfoVo> infoUser(){
       UserDetailInfoVo user =  userService.queryUserDetaillnfoVoById(AuthUtil.getLoginUserId());
        return R.OK(user);
    }

    /**
     * 判断后台密码与前端密码是否一样
     * @return
     */
    @GetMapping("/backstagePwd")
    public R<Boolean> backstagePwd(@RequestParam("oldPwd") String pwd){
        SecurityUser user = AuthUtil.getSecurityUser();
        return R.OK(passwordEncoder.matches(pwd,user.getLoginPwd()));
    }
    /**
     * 修改密码
     * @return
     */
    @PreAuthorize("hasAuthority('user:edit')")
    @PutMapping("updataPwd")
    public R<String> updataPwd(@RequestBody User user){
          Boolean result =   userService.updataPwd(user);
        return R.HANDLER(result);
    }
    /**
     * 登录成功
     * @return
     */
    @GetMapping("/index")
    public R<String> login(){
        return R.OK("登录成功");
    }
    /**
     * 分页查询用户信息
     * @return
     */
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/page")
    public R<PageInfo<User>> pageInfo(@RequestParam("current") Integer current,
                                      @RequestParam(defaultValue = "10")  Integer size){
           PageInfo<User> pageInfo =  userService.queryUserListPage(current,size);
        return R.OK(pageInfo);
    }

    /**
     * 查询用户详细请
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R<UserDetailInfoVo> detail(@PathVariable("id") Integer id){
        UserDetailInfoVo userDetailInfoVo = userService.queryUserDetaillnfoVoById(id);
        return R.OK(userDetailInfoVo);
    }

    /**
     * 查询id用户的信息
     * @return
     */
    @GetMapping("/infoUser1/{id}")
    public R<UserDetailInfoVo> queryUserId(@PathVariable("id") Integer id){
        UserDetailInfoVo userDetailInfoVo = userService.queryUserDetaillnfoVoById(id);
        return R.OK(userDetailInfoVo);
    }

    /**
     * 修改信息
     * @param user
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody User user){
        Boolean result =   userService.modifyUser(user);
        return R.HANDLER(result);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping
    public R<String> add(@RequestBody User user){
        Boolean result =   userService.saveUser(user);
        return R.HANDLER(result);
    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> removeUser(@PathVariable("id") Integer id){
        Boolean result = null;
        try {
            result = userService.removeUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
            return R.FAIL("删除失败");
        }
        return R.HANDLER(result);
    }
    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @DeleteMapping("/batch/{ids}")
    public R<String> removeUserBatch(@PathVariable("ids") List<Integer> ids){

        Boolean result = userService.removeUserBatchByIds(ids);
        return R.HANDLER(result);
    }
    /**
     * 查询所有用户列表
     * @return
     */
    @GetMapping("/list")
    public R<List<User>> loadAllUsers() {
        List<User> users = userService.queryAllUsers();
        return R.OK(users);
    }
    /**
     * 查询所有用户名字和角色
     * @return
     */
    @GetMapping("/list2")
    public R<List<UserVo>> loadAllUserAndRole() {
        List<UserVo> users = userService.queryAllUserVos();
        return R.OK(users);
    }
    /**
     * 查询id用户名字和角色
     * @return
     */
    @GetMapping("/role/{id}")
    public R<UserVo> loadUserAndRole(@PathVariable Integer id) {
        UserVo user = userService.queryUserVoRoleById(id);
        return R.OK(user);
    }

    @GetMapping("/userAct")
    public R<List<PieData>> loadUserAct() {
        List<PieData> list = userService.queryUserAct();
        return R.OK(list);
    }
}
